//
//  AddBankCardViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/22.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "AddBankCardViewController.h"
#import "AddBankCardCell.h"
#import "SectionTitleReusableView.h"
#import "AddDepositCardViewController.h"
#import "AddCreditCardViewController.h"
#import "SupportBankListViewController.h"
#import "RiskEnsureController.h"

static NSString * const AddBankCardReuseId = @"AddBankCardCell";
static NSString * const SectionTitleReuseId = @"SectionTitleReusableView";
static NSString * const FootViewReuseId = @"FootViewReuseId";

@interface AddBankCardViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray   *mutableDataArr;
/** 储蓄卡 */
@property (nonatomic, strong) NSIndexPath *selectCashCard;
/** 信用卡 */
@property (nonatomic, strong) NSIndexPath *selectCreditCard;
@end

@implementation AddBankCardViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = _bankType ? @"储蓄卡" : @"选择银行卡";
//    _selectCreditCard = [NSIndexPath indexPathForRow:0 inSection:1];
//    _selectCashCard = [NSIndexPath indexPathForRow:0 inSection:0];
    
    [self.view addSubview:self.collectionView];
    [self registerCell];
    
    [self addRightButton];
    [self getdata];
}
- (void)getdata
{
    //储蓄卡
    [XYHttpRequest dataRequestWithMyBankCardListType:0 success:^(id responseObj) {
        
        if (Success) {
            NSArray * arr = responseObj[@"data"][@"result"];
            
            [self.mutableDataArr addObject:arr];
        }
        //信用卡
        [XYHttpRequest dataRequestWithMyBankCardListType:1 success:^(id responseObj) {
            
            if (Success) {
                NSArray * arr = responseObj[@"data"][@"result"];
                
                [self.mutableDataArr addObject:arr];
                
                [self.collectionView reloadData];
            }
        } failure:^(NSError *error) {
            
        }];
    } failure:^(NSError *error) {
        
    }];
    
    
}
- (void)registerCell
{
    [_collectionView registerNib:[UINib nibWithNibName:AddBankCardReuseId bundle:nil] forCellWithReuseIdentifier:AddBankCardReuseId];
    [_collectionView registerNib:[UINib nibWithNibName:SectionTitleReuseId bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SectionTitleReuseId];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FootViewReuseId];
}

#pragma mark - collectionView Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [self.mutableDataArr count];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.mutableDataArr[section] count] + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AddBankCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AddBankCardReuseId forIndexPath:indexPath];
    
    if ([self.mutableDataArr[indexPath.section] count] == 0) {
        
        [cell showAddCard];
        return cell;
    }else{
        if (indexPath.item + 1 > [self.mutableDataArr[indexPath.section] count]) {
            
            [cell showAddCard];
            return cell; 
        }else{
            
            NSDictionary *dic = self.mutableDataArr[indexPath.section][indexPath.row];
            
            [cell configData:dic];
            if (indexPath.section == 0) {
                
                if (!_selectCashCard) {
                    
                    //赋值
                    if ([dic[@"defaultCard"]integerValue]) {
                        
                        _selectCashCard = indexPath;
                        
                        [cell showChoosed];
                    }else{
                        [cell showDefault];
                    }
                    

                }else{
                    
                    //判断状态
                    if (indexPath.row == _selectCashCard.row) {
                        
                        [cell showChoosed];
                        
                    }else{
                        
                        [cell showDefault];
                    }
                }
                
                
            }else{
                
                if (!_selectCreditCard) {
                    //赋值
                    if ([dic[@"defaultCard"]integerValue]) {
                        
                        _selectCreditCard = indexPath;
                        [cell showChoosed];
                    }else{
                        [cell showDefault];
                    }
                }else{
                    //判断状态
                    if (indexPath.row == _selectCreditCard.row) {
                        [cell showChoosed];
                        
                    }else{
                        [cell showDefault];
                    }
                }
                
            }
            
            return cell;
        }
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{
    if (kind == UICollectionElementKindSectionHeader) {
        SectionTitleReusableView *sectionView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SectionTitleReuseId forIndexPath:indexPath];
        sectionView.titleLb.text = @[@"还款关联的储蓄卡",@"借款关联的储蓄卡"][indexPath.section];
        return sectionView;
    }else{
        
        if (self.bankType == AddBankChoose) {
            UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FootViewReuseId forIndexPath:indexPath];
            
            //下一步
            UIButton *nextBtn = [Factory customButtonWithTitle:@"下一步"];
            nextBtn.frame = CGRectMake(35, 10, kScreenWidth - 70, 60);
            [nextBtn addTarget:self action:@selector(clickMethod:) forControlEvents:UIControlEventTouchUpInside];
            [footView addSubview:nextBtn];
            
            return footView;
        }else{
            return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FootViewReuseId forIndexPath:indexPath];
        }
        
    }
    
}

- (void)clickMethod:(UIButton *)sender
{
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake((kScreenWidth - 75) / 2, 95);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreenWidth, 50);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return CGSizeMake(kScreenWidth, 80);
    }else{
        return CGSizeMake(kScreenWidth, 0);
    }
}

#pragma mark  定义每个UICollectionView的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark  定义每个UICollectionView的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 25, 10, 25);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == [self.mutableDataArr[indexPath.section] count]) {
        NSLog(@"添加");
        if (indexPath.section == 0) {
            AddDepositCardViewController *vc = [[AddDepositCardViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if(indexPath.section == 1) {
            AddCreditCardViewController *vc = [[AddCreditCardViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }else{
       
        if (indexPath.section == 0) {
            
            AddBankCardCell *select = (AddBankCardCell *)[collectionView cellForItemAtIndexPath:_selectCashCard];
            select.checkIV.hidden = YES;
            
            _selectCashCard = indexPath;
            
        }else{
            AddBankCardCell *select = (AddBankCardCell *)[collectionView cellForItemAtIndexPath:_selectCreditCard];
            select.checkIV.hidden = YES;
            
            _selectCreditCard = indexPath;
        }
        AddBankCardCell *cell = (AddBankCardCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.checkIV.hidden = NO;
        NSLog(@"不添加");
    }
}

//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    AddBankCardCell *cell = (AddBankCardCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.checkIV.hidden = YES;
//    
//}

- (void)addRightButton
{
    UIButton *btn = [UIButton ba_buttonWithFrame:CGRectMake(0, 0, 80, 64) title:@"支持银行卡" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:13]];
    [btn ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeRightImageLeft padding:2.0f];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    btn.frame = CGRectMake(0, 0, 80, 64);
    //把视图的边角变为圆形, cornerRadius圆角半径
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    //弹簧控件, 修复边距
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -5;
    self.navigationItem.rightBarButtonItems = @[spaceItem,backItem];
    [btn addTarget:self action:@selector(clickRule:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickRule:(UIButton *)sender
{
    SupportBankListViewController *support = [[SupportBankListViewController alloc]init];
    support.menuViewStyle = WMMenuViewStyleLine;
    [self.navigationController pushViewController:support animated:YES];
}


#pragma mark - 懒加载  Lazy Load
- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = YES;//注册Cell
        _collectionView.showsVerticalScrollIndicator = NO;
    }
    return _collectionView;
}

- (NSMutableArray *)mutableDataArr
{
    if (_mutableDataArr == nil) {
        _mutableDataArr = [NSMutableArray array];
//        NSMutableArray *arr = @[@"2333",@"2333",@"2333",@"2333",@"2333",@"2333"].mutableCopy;
//        NSMutableArray *arr1 = @[@"2333",@"2333"].mutableCopy;
//        [_mutableDataArr addObject:arr];
//        [_mutableDataArr addObject:arr1];
    }
    return _mutableDataArr;
}

@end
